package ca.usherbrooke.view.properties;

import java.io.IOException;

import org.newdawn.slick.Color;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;

public class EquipmentViewProperties {
	private static final String SPRITE_SHEET_PATH = "sprite_sheet_path_key";
	private static final String SPRITE_SHEET_ENTITY_WIDTH = "sprite_sheet_entity_width";
	private static final String SPRITE_SHEET_ENTITY_HEIGHT = "sprite_sheet_entity_height";
	private static final String SPRITE_TRANSPARENCE_R = "transparence_color_r";
	private static final String SPRITE_TRANSPARENCE_G = "transparence_color_g";
	private static final String SPRITE_TRANSPARENCE_B = "transparence_color_b";
	private static final String DEFAULT_IMG = "default";

	private final PropertieBase properties;
	private final SpriteSheet characterSheet;
	private final int defaultId;

	public EquipmentViewProperties(String fileName) throws SlickException,
			IOException {
		properties = new PropertieBase(fileName);

		float r = properties.getFloatValue(SPRITE_TRANSPARENCE_R);
		float g = properties.getFloatValue(SPRITE_TRANSPARENCE_G);
		float b = properties.getFloatValue(SPRITE_TRANSPARENCE_B);
		Color color = new Color(r, g, b);
		characterSheet = new SpriteSheet(properties.getPath(SPRITE_SHEET_PATH),
				properties.getIntegerValue(SPRITE_SHEET_ENTITY_WIDTH),
				properties.getIntegerValue(SPRITE_SHEET_ENTITY_HEIGHT), color);

		defaultId = properties.getIntegerValue(DEFAULT_IMG);
	}

	/**
	 * 
	 * @return the Image
	 */
	protected Image generateImage(int id) {
		int xPos = (id % 10);
		int yPos = Double.valueOf(Math.floor(id / 10)).intValue();
		return characterSheet.getSprite(xPos, yPos);
	}

	private Integer getId(String key) {
		return properties.getIntegerValue(key, defaultId);
	}

	public Image getImage(String key) {

		return generateImage(getId(key));
	}
}
